const { db, genid } = require('../database/index');
const { v4: uuidv4 } = require('uuid');

const loginHandler = {
  // 登录
  async enter(req, res) {
    const { login, password } = req.body;
    sqlSelectStr = `SELECT * FROM 
                    managers 
                    where login = '${login}' 
                    AND password = ${password}`;
    const out = await db.async(sqlSelectStr);
    if (out.err) {
      return res.jgcl('登陆失败！' + out.err, 500);
    }

    let login_token = uuidv4();
    const sqlUpdateStr = `UPDATE managers 
                            SET token = '${login_token}'
                            where id = ${out[0].id}`;
    const outp = await db.async(sqlUpdateStr);
    if (outp.err) return res.jgcl(out.err, 500);
    let login_info = out[0];
    login_info.token = login_token;
    login_info.password = '不能给你！'
    res.jgcl('登录成功！', 200, login_info)


  }
}

module.exports = loginHandler